package com.cxyd.modules.wcs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.ConvertUtils;
import com.cxyd.modules.wcs.dao.PcItemDao;
import com.cxyd.modules.wcs.dao.PlcItemDao;
import com.cxyd.modules.wcs.dto.PlcItemDTO;
import com.cxyd.modules.wcs.entity.PcItemEntity;
import com.cxyd.modules.wcs.entity.PlcItemEntity;
import com.cxyd.modules.wcs.service.PlcItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PlcItemServiceImpl extends CrudServiceImpl<PlcItemDao, PlcItemEntity, PlcItemDTO> implements PlcItemService {
    @Resource
    private PcItemDao pcItemDao;

    @Override
    public List<PlcItemDTO> selectAll() {
        List<PlcItemEntity> entities = baseDao.selectAll();
        return ConvertUtils.sourceToTarget(entities, PlcItemDTO.class);
    }

    @Override
    public boolean signalJudgeByNo(String itemNo, String signal) {
        QueryWrapper<PlcItemEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(PlcItemEntity::getItemNo, itemNo)
                .eq(PlcItemEntity::getItemValue, signal).last("LIMIT 1");
        PlcItemEntity item = baseDao.selectOne(qw);
        if (item == null) {
            return false;
        }
        return true;
    }

    @Override
    public PlcItemEntity getItemByUnitNo(String unitNo, String itemValue) {
        QueryWrapper<PlcItemEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(PlcItemEntity::getUnitNo, unitNo)
                .eq(PlcItemEntity::getItemValue, itemValue)
                .like(PlcItemEntity::getItemGrp, "TASK").last("LIMIT 1");
        return baseDao.selectOne(qw);
    }

    @Override
    public QueryWrapper<PlcItemEntity> getWrapper(Map<String, Object> params) {
        String itemNo = (String) params.get("itemNo");
        String itemValue = (String) params.get("itemValue");
        String unitNo = (String) params.get("unitNo");
        String itemName = (String) params.get("itemName");
        String itemGrp = (String) params.get("itemGrp");
        String itemAdr = (String) params.get("itemAdr");
        String eventNo = (String) params.get("eventNo");
        QueryWrapper<PlcItemEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(itemAdr), "item_adr", itemAdr);
        wrapper.eq(StringUtils.isNotBlank(itemValue), "item_value", itemValue);
        wrapper.like(StringUtils.isNotBlank(itemNo), "item_no", itemNo);
        wrapper.like(StringUtils.isNotBlank(unitNo), "unit_no", unitNo);
        wrapper.like(StringUtils.isNotBlank(itemGrp), "item_grp", itemGrp);
        wrapper.like(StringUtils.isNotBlank(itemName), "item_name", itemName);
        wrapper.like(StringUtils.isNotBlank(eventNo), "event_no", eventNo);
        return wrapper;
    }

    @Override
    public void update(PlcItemDTO dto) {
//        更新PLC_ITEM中的值实际是向PC_ITEM中写入信号
        PcItemEntity entity = ConvertUtils.sourceToTarget(dto, PcItemEntity.class);
        entity.setStatus(1);
        entity.setUpdateDate(new Date());
        pcItemDao.updateById(entity);
    }
}
